#include <iostream>
#include <stdio.h>
#include <chrono>
#include <thread>

#include "kfa_demux.h"


#include "arg_parser.h"
#include "kfa_player.h"

#define LOGD printf
#define LOGE printf
#define LOGI printf
using namespace std;




static uint64_t NowMs()
{
    return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
static void SleepMs(uint32_t ms)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}

#if 1
static int play_kfa(const std::string& kfa_file, render_param& param)
{
    KfaPlayer player;
    bool ret = player.setDataSource(kfa_file);

    if (!ret)
    {
        //LOGE("")
        return -1;
    }

    ret = player.setPlayerParameter(param);

    if (!ret)
    {
        return -1;
    }

    ret = player.start();

    if (!ret)
    {
        return -1;
    }

    while (player.is_playing())
    {
        SleepMs(10);
    }
    LOGD("[%s.%d]===>:\n", __FUNCTION__, __LINE__);
    return 0;
}
#else
static int play_kfa(const std::string& kfa_file, render_param& param)
{

}

#endif

int main(int argc, char** argv)
{
    cout << "hello world!" << endl;
    std::string file_path;
    render_param init_param;

    icpp::core::ArgParser a;

    a.addWithParamNoneHandler("help",'h', "show this usage info", [&a]() -> icpp::core::ArgumentParseError
    {
        a.showHelp();
        return icpp::core::ArgumentParseError::kDoCommand;
    });

    a.addWithParamRequired<std::string>("file", 'f', "the file of *.kfa", true, file_path);
    
    a.addWithParamNone("repeat", 'r', "is repeat play the current animation", init_param.repeat);
    a.addWithParamRequired<uint32_t>("interval", 'i', "the duration of frame interval, in milli-second", false, init_param.frame_interval_ms, 40);
    a.addWithParamRequired<int32_t>("display", 'd', "the display id to show the animation", false, init_param.display_id, 1);
    a.addWithParamRequired<int32_t>("zorder", 'z', "the window zorder of the animation", false, init_param.zorder, 1000);
    //a.addWithParamRequired<int32_t>("type", 't', "the decode type, 1: spng, 2: qnx img", false, dec_type, 1);

    icpp::core::ArgumentParseError ret = a.onParse(argc, argv);

    if (icpp::core::ArgumentParseError::kSuccess == ret)
    {
        play_kfa(file_path, init_param);       
    }
    else{
        //a.showHelp();
    }




    return 0;
}
